﻿Public Class clsLHJWorkDetail
    Private tempval As String = "temp_val_1,temp_val_2,temp_val_3,temp_val_4,temp_val_5,
    temp_val_6,temp_val_7,temp_val_8,temp_val_9,temp_val_10,
    temp_val_11,temp_val_12,temp_val_13,temp_val_14,temp_val_15,
    temp_val_16,temp_val_17,temp_val_18,temp_val_19,temp_val_20"

    ''' <summary>
    ''' 折线图数据查询
    ''' </summary>
    ''' <param name="lhj_id">lhj_id</param>
    ''' <param name="prod_id">生产编号</param>
    ''' <returns></returns>
    Public Function selectLHJWorkDetailInfoDESC(lhj_id As String, prod_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT prod_id," & tempval & ",pres_val1,pres_val2,col_time,status,lh_time_len,lhsysj,zsc,lhjsc,old_status "
        szSql &= " FROM sf.b_lhj_work_detail WHERE factory_id=? and prod_id=? and lhj_id = ? ORDER BY col_time DESC"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, prod_id, lhj_id)
        If IsNothing(dt) Then
            selectLHJWorkDetailInfoDESC = New DataTable
        Else
            selectLHJWorkDetailInfoDESC = dt
        End If
    End Function

    Public Function insertData(en As entity_LHDataInput) As Integer

        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " INSERT INTO sf.b_lhj_work_detail( factory_id , prod_id , lhj_id , col_time , "
        szSql &= tempval
        szSql &= " , pres_val1, pres_val2 , update_time , lh_time_len, status,zsc,lhjsc,lhsysj,old_status )  "
        szSql &= " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  "

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, en.factory_id, en.prod_id, en.lhj_id, en.col_time,
        en.temp_val_1, en.temp_val_2, en.temp_val_3, en.temp_val_4, en.temp_val_5,
        en.temp_val_6, en.temp_val_7, en.temp_val_8, en.temp_val_9, en.temp_val_10,
        en.temp_val_11, en.temp_val_12, en.temp_val_13, en.temp_val_14, en.temp_val_15,
        en.temp_val_16, en.temp_val_17, en.temp_val_18, en.temp_val_19, en.temp_val_20,
        en.pres_val1, en.pres_val2, en.update_time, en.lh_time_len, en.status, en.zsc, en.lhjsc, en.lhsysj, en.old_status)

        Return rtn

    End Function

    Public Function selectDetailByLimit(lhj_id As String, prod_id As String, _status As Integer, rownum As Integer) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM sf.b_lhj_work_detail "
        szSql &= "WHERE factory_id=? and prod_id=? and lhj_id = ? and status =? ORDER BY col_time DESC limit " & rownum
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, prod_id, lhj_id, _status)
        If IsNothing(dt) Then
            selectDetailByLimit = New DataTable
        Else
            selectDetailByLimit = dt
        End If
    End Function
    Public Function chkJR(lhj_id As String, prod_id As String, _status As Integer, jrsj As Integer, _limitRows As Integer) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer = 0
        '得到第1个2阶段收集时间（A）
        szSql = "SELECT * FROM sf.b_lhj_work_detail "
        szSql &= "WHERE factory_id=? and prod_id=? and lhj_id = ? and status =? ORDER BY update_time asc limit 1"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, prod_id, lhj_id, _status)
        If IsNothing(dt) OrElse dt.Rows.Count = 0 Then
            '如果=0条，不进行判断(后面的判断也不进行）
            rtn = 0
        Else
            '得到A+加热时间的数据 
            Dim tmJR As DateTime = DateAdd(DateInterval.Minute, jrsj, dt(0)("col_time"))

            szSql = "SELECT * FROM sf.b_lhj_work_detail "
            szSql &= "WHERE factory_id=? and prod_id=? and lhj_id = ? and status =? and col_time>? "
            szSql &= "ORDER BY update_time desc"
            Dim dtSearchResult As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, prod_id, lhj_id, _status, tmJR)
            If IsNothing(dtSearchResult) OrElse dtSearchResult.Rows.Count < _limitRows Then
                '如果=0条，不进行判断(后面的判断也不进行）
                rtn = 0
            Else
                'If dt1.Rows.Count = 1 Then
                '    '如果=1条，判断是否在区间内
                '    rtn = 1
                'Else
                If dtSearchResult.Rows.Count > _limitRows Then
                    '如果>1条，则进行后面的判断
                    rtn = 2
                End If
            End If
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' 准备出炉的时间（单位：分钟）
    ''' </summary>
    ''' <returns></returns>
    Public Function getReadyOutMinute() As Integer
        Dim rtn As Integer = 30
        Dim szSql As String = ""
        szSql = "SELECT param_value FROM sf.t_sys_param "
        szSql &= "WHERE param_key='LH_Ready_Out_Minute' "
        Dim dtsys As DataTable = DBacs.ExecuteSql(szSql, -1)
        If dtsys.Rows.Count > 0 Then
            rtn = CInt(dtsys(0)(0))
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' 硫化持续次数
    ''' </summary>
    ''' <returns></returns>
    Public Function getErrorContinuoustimes() As Integer
        Dim rtn As Integer = 5
        Dim szSql As String = ""
        szSql = "SELECT param_value FROM sf.t_sys_param "
        szSql &= "WHERE param_key='LH_Error_Continuous times' "
        Dim dtsys As DataTable = DBacs.ExecuteSql(szSql, -1)
        If dtsys.Rows.Count > 0 Then
            rtn = CInt(dtsys(0)(0))
        End If

        Return rtn
    End Function

    Public Function insertErr(en As entity_LHDataInput, _type_id As Integer, _err_type As String, _err_content As String) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        Dim LH_Error_Report_interval_Minute As Integer = 15 '间隔时长

        '取得系统间隔时间
        szSql = "SELECT param_value FROM sf.t_sys_param "
        szSql &= "WHERE param_key='LH_Error_Report_interval_Minute' "
        Dim dtsys As DataTable = DBacs.ExecuteSql(szSql, -1)
        If dtsys.Rows.Count > 0 Then
            LH_Error_Report_interval_Minute = dtsys(0)(0)
        End If

        '计算是否在规定时间内，如果未超过规定时间，则不报出相同报警
        szSql = "select err_dt from sf.b_lh_error_history "
        szSql &= "where  factory_id=? and  prod_id=? and type_id=? and err_type=? "
        szSql &= "order by err_dt desc limit 1"
        Dim dtErrdt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, en.prod_id, _type_id, _err_type)
        If dtErrdt.Rows.Count > 0 Then
            Dim oldDt As DateTime = CDate(dtErrdt(0)(0))
            If DateDiff(DateInterval.Minute, oldDt, CDate(en.update_time)) <= 15 Then
                Return 0
            End If
        End If


        szSql = "INSERT INTO sf.b_lh_error_history( factory_id , prod_id , lhj , status , type_id , err_dt , err_type , err_content , handled , update_time)"
        szSql &= " VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)"

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, g_factory_id, en.prod_id, en.lhj_id, en.status, _type_id, en.update_time, _err_type, _err_content, 0, Now())

        Return rtn
    End Function

    Public Sub dataMoveToHistory(dtNow As DateTime)
        ''2021/10/23 00:00:00'
        Dim strYMD As String = dtNow.Year & "/" & dtNow.Month & "/" & dtNow.Day
        Dim sql As String = String.Empty
        Dim existFlg As Boolean = True
        Dim newTableName As String = "b_lhj_work_detail_" & dtNow.Year & dtNow.Month.ToString.PadLeft(2, "0")
        Dim newTableNameWitSF As String = "sf." & newTableName
        sql = "select count(*) countExist from information_schema.tables where table_schema='sf'"
        sql &= " and table_type='BASE TABLE' and table_name=?;"
        Dim dtExist As DataTable = DBacs.ExecuteSql(sql, -1, newTableName)
        '判断是否有当月的表
        If dtExist.Rows.Count > 0 Then
            If dtExist.Rows(0)("countExist") > 0 Then
                existFlg = True
            Else
                existFlg = False
            End If
        End If

        '如果有当天完成的支座，就查询出数据后，往履历表里插入，然后删除原数据
        sql = "select count(prod_id) countComp  from sf.b_prod_info where comp_time between '" & strYMD & " 00:00:00' and '" & strYMD & " 23:59:59' "
        Dim dtComp As DataTable = DBacs.ExecuteSql(sql, -1, newTableName)
        If dtComp.Rows.Count > 0 Then
            If dtComp.Rows(0)("countComp") > 0 Then
                If existFlg = False Then
                    sql = " Select a.* INTO " & newTableNameWitSF & " from sf.b_lhj_work_detail a inner join "
                    sql &= "(select factory_id,prod_id  from sf.b_prod_info "
                    sql &= "where comp_time between '" & strYMD & " 00:00:00' and '" & strYMD & " 23:59:59') b "
                    sql &= "On a.factory_id=b.factory_id And a.prod_id=b.prod_id "
                    sql &= "order by a.prod_id "
                    DBacs.ExecuteUpdateSql(sql, -1)
                Else
                    sql = " insert into " & newTableNameWitSF
                    sql &= " select a.* from sf.b_lhj_work_detail a inner join "
                    sql &= "(select factory_id,prod_id  from sf.b_prod_info "
                    sql &= "where comp_time between '" & strYMD & " 00:00:00' and '" & strYMD & " 23:59:59') b "
                    sql &= "on a.factory_id=b.factory_id and a.prod_id=b.prod_id "
                    sql &= "order by a.prod_id "
                    DBacs.ExecuteUpdateSql(sql, -1)
                End If

                '删除原表数据
                sql = "delete from  sf.b_lhj_work_detail "
                sql &= "where prod_id in (select prod_id  from sf.b_prod_info "
                sql &= "where comp_time between '" & strYMD & " 00:00:00' and '" & strYMD & " 23:59:59')"
                DBacs.ExecuteUpdateSql(sql, -1)
            End If
        End If
    End Sub


End Class

Public Class entity_LHDataInput
    Public factory_id As String
    Public prod_id As String
    Public lhj_id As String
    Public col_time As String
    Public status As String
    Public temp_val_1 As Double
    Public temp_val_2 As Double
    Public temp_val_3 As Double
    Public temp_val_4 As Double
    Public temp_val_5 As Double
    Public temp_val_6 As Double
    Public temp_val_7 As Double
    Public temp_val_8 As Double
    Public temp_val_9 As Double
    Public temp_val_10 As Double
    Public temp_val_11 As Double
    Public temp_val_12 As Double
    Public temp_val_13 As Double
    Public temp_val_14 As Double
    Public temp_val_15 As Double
    Public temp_val_16 As Double
    Public temp_val_17 As Double
    Public temp_val_18 As Double
    Public temp_val_19 As Double
    Public temp_val_20 As Double

    Public pres_val1 As Double
    Public pres_val2 As Double
    Public lh_time_len As Integer
    Public lhsysj As Integer
    Public zsc As Integer
    Public lhjsc As Integer
    Public update_time As String
    Public old_status As String
End Class
